@extends('admin.layout.base')
@section('styles')
    <style>
        .ad-img {
            width: 210px;
            height: 140px;
            border: 1px dashed #d9d9d9;
            display: block;
        }
        .ad-uploader-icon {
            cursor: pointer;
            font-size: 28px;
            color: #8c939d;
            width: 210px;
            height: 140px;
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            line-height: 150px;
            text-align: center;
        }
    </style>
@endsection
@section('content')
    <div class="panel-content" id="app" v-cloak>
        <div class="panel-body panel-info">
            <el-form :model="form" ref="form" class="form" label-width="100px">
                <div class="panel-header"> <div class="panel-title"> 基本信息 </div> </div>
                <div class="panel-main">
                    <el-form-item :rules="[{ required: true, message: '请填写服务名', trigger: 'blur'}]" label="服务名：" prop="name">
                        <el-input clearable v-model="form.name" placeholder="请填写服务名"></el-input>
                    </el-form-item>
                    <el-form-item label="店铺：" prop="store_id">
                        <el-input readonly v-model="form.store_name"></el-input>
                        <el-input readonly v-model="form.store_id" type="hidden"></el-input>
                    </el-form-item>
                    <el-form-item :rules="[{ required: true, message: '请选择类目', trigger: 'blur'}]" label="类目：" prop="cate_id">
                        <el-cascader placeholder="请选择类目"
                                     separator=">"
                                     :options="categoys"
                                     expand-trigger="hover"
                                     v-model="form.cate_id">
                        </el-cascader>
                    </el-form-item>
                    <el-form-item :rules="[{ required: true, message: '请上传封面图', trigger: 'blur'}]" label="封面图：" prop="thumb">
                        <el-upload class="ad-uploader" name="image"
                                :action="upload_image"
                                :show-file-list="false"
                                :limit="1"
                                :on-success="uploadthumbSuccess"
                                :before-upload="uploadthumbBefore">
                            <img v-if="form.thumb" :src="form.thumb" class="ad-img">
                            <i v-else class="el-icon-plus ad-uploader-icon"></i>
                            <div slot="tip" class="el-upload__tip">图片大小不能超过 2MB，建议尺寸3:2!</div>
                        </el-upload>
                    </el-form-item>
                    <el-form-item label="价格：" prop="price">
                        <el-input clearable v-model="form.price" placeholder="请填写服务价格"></el-input>
                    </el-form-item>
                </div>
                <div class="panel-header"> <div class="panel-title"> 其他信息 </div> </div>
                <div class="panel-main">
                    <el-form-item label="服务图：" prop="images">
                        <el-upload list-type="picture-card" name="image" multiple
                            :action="upload_image"
                            :on-success="uploadimgSuccess"
                            :before-upload="uploadimgBefore"
                            :on-preview="handlePictureCardPreview"
                            :on-remove="handleRemove"
                            :file-list="img_arr" >
                            <i class="el-icon-plus"></i>
                            <i v-else class="el-icon-plus ad-uploader-icon"></i>
                            <div slot="tip" class="el-upload__tip">图片大小不能超过 2MB，建议尺寸3:2!</div>
                        </el-upload>
                        <el-dialog :visible.sync="dialogVisible">
                            <img width="100%" :src="dialogImageUrl" alt="">
                        </el-dialog>
                    </el-form-item>
                    <el-form-item :rules="[{ max: 100, message: '服务简介的长度不能超过100个字', trigger: 'blur' }]" label="简介：" prop="description">
                        <el-input type="textarea" rows="5" v-model="form.description"></el-input>
                    </el-form-item>
                    <el-form-item :rules="[{ required: true, message: '请填写服务详情', trigger: 'blur'}]" label="描述：" prop="content">
                        <div id="editor"></div>
                    </el-form-item>
                    <el-form-item label="服务区域：" prop="area">
                        <el-cascader placeholder="请选择服务区域"
                                     separator=">"
                                     :options="areas"
                                     expand-trigger="hover"
                                     v-model="form.area">
                        </el-cascader>
                    </el-form-item>
                    <el-form-item label="联系人：" prop="call_name">
                        <el-input clearable v-model="form.call_name" placeholder="请填写联系人"></el-input>
                    </el-form-item>
                    <el-form-item label="联系电话：" prop="call_phone">
                        <el-input @blur="phone" clearable v-model="form.call_phone" placeholder="请填写联系电话"></el-input>
                    </el-form-item>
                    <el-form-item label="部门：" prop="department">
                        <el-input clearable v-model="form.department" placeholder="请填写部门"></el-input>
                    </el-form-item>
                    <el-form-item label="职位：" prop="position">
                        <el-input clearable v-model="form.position" placeholder="请填写职位"></el-input>
                    </el-form-item>
                    <el-form-item label="来电量：" prop="call">
                        <el-input clearable v-model="form.call" placeholder="请填写来电量"></el-input>
                    </el-form-item>
                    <el-form-item label="浏览量：" prop="view">
                        <el-input clearable v-model="form.view" placeholder="请填写浏览量"></el-input>
                    </el-form-item>
                    <el-form-item label="中标量：" prop="salenum">
                        <el-input clearable v-model="form.salenum" placeholder="请填写中标量"></el-input>
                    </el-form-item>
                    <el-form-item label="收藏量：" prop="collect">
                        <el-input clearable v-model="form.collect" placeholder="请填写收藏量"></el-input>
                    </el-form-item>
                    <el-form-item label="星级：" prop="star">
                        <el-input clearable v-model="form.star" placeholder="请填写星级"></el-input>
                    </el-form-item>
                    <el-form-item label="好评率：" prop="credit">
                        <el-input clearable v-model="form.credit" placeholder="请填写好评率"></el-input>
                    </el-form-item>
                    {{--<el-form-item label="服务类型：" prop="type">--}}
                        {{--<el-input clearable v-model="form.type" placeholder="请填写服务类型"></el-input>--}}
                    {{--</el-form-item>--}}
                    <el-form-item required label="推荐：" required prop="recommend">
                        <el-radio-group v-model="form.recommend">
                            <el-radio :label="1" border>正常</el-radio>
                            <el-radio :label="2" border>推荐</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item required label="状态：" required prop="status">
                        <el-radio-group v-model="form.status">
                            <el-radio :label="1" border>正常</el-radio>
                            <el-radio :label="2" border>待审核</el-radio>
                            <el-radio :label="3" border>未通过</el-radio>
                            <el-radio :label="4" border>下架</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </div>
                <el-form-item style="padding-left: 150px;margin-top: 50px">
                    <el-button type="primary" @click="update()">确 定</el-button>
                    <el-button @click="back()" style="margin-left: 50px">返 回</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
@endsection
@section('scripts')
    <script src="{{ asset('neditor/neditor.config.js') }}" type="text/javascript"></script>
    <script src="{{ asset('neditor/neditor.all.min.js') }}" type="text/javascript"></script>
    <script src="{{ asset('neditor/neditor.service.js') }}" type="text/javascript"></script>
    <script src="{{ asset('neditor/i18n/zh-cn/zh-cn.js') }}" type="text/javascript"></script>
    <script src="{{ asset('neditor/third-party/browser-md5-file.min.js') }}" type="text/javascript"></script>
    <script src="{{ asset('neditor/third-party/jquery-1.10.2.min.js') }}" type="text/javascript"></script>
    <script>
        $(function () {
            let app = new Vue({
                el: '#app',
                data: function () {
                    return {
                        dialogImageUrl: '',
                        dialogVisible: false,
                        info: {!! $info !!},
                        upload_image: UPLOAD_IMAGE,
                        form: {
                            id: "{{ $id }}",
                            store_id: "{{ $store['id'] }}",
                            store_name: "{{ $store['store_name'] }}",
                            cate_id: '',
                            name: '',
                            thumb: '',
                            images: [],
                            price: 0.00,
                            description: '',
                            content: '',
                            call_name: '',
                            call_phone: '',
                            department: '',
                            position: '',
                            area: 0,
                            view: 0,
                            call: 0,
                            salenum: 0,
                            collect: 0,
                            star: 5,
                            credit: 0.00,
                            type: '',
                            recommend: 1,
                            status: 2,
                        },
                        editor: null,
                        categoys: {!! $categorys !!},
                        areas: {!! $areas !!},
                        img_arr:[],
                    }
                },
                created() {
                    this.editor = UE.getEditor('editor');
                    let that = this;
                    if (this.form.id > 0) {
                        this.form = {
                            id: this.info.id,
                            store_id: this.info.store_id,
                            store_name: "{{ $store['store_name'] }}",
                            cate_id: this.info.cate_id,
                            name: this.info.name,
                            thumb: this.info.thumb,
                            images: this.info.images,
                            price: this.info.price,
                            description: this.info.description,
                            content: this.info.content,
                            call_name: this.info.call_name,
                            call_phone: this.info.call_phone,
                            department: this.info.department,
                            position: this.info.position,
                            area: this.info.area,
                            view: this.info.view,
                            call: this.info.call,
                            salenum: this.info.salenum,
                            collect: this.info.collect,
                            star: this.info.star,
                            credit: this.info.credit,
                            type: this.info.type,
                            recommend: this.info.recommend,
                            status: this.info.status,
                        };
                        this.editor.ready(function () {
                            that.editor.setContent(that.form.content);
                        });
                    }
                    this.editor.ready(function () {
                        that.editor.setPlaceholder('请填写服务详情');
                    });
                    this.form.images.map(item=>{
                        this.img_arr.push({url:item})
                    })
                },
                methods: {
                    //验证电话号码
                    phone(){
                        if(!(/^1[3456789]\d{9}$/.test(this.form.call_phone))){
                            return this.$message.error('手机号码有误，请重填!');
                            return false;
                        }
                    },
                    //上传图片
                    uploadthumbSuccess(res) {
                        this.form.thumb = res.data.path;
                    },
                    uploadthumbBefore(file) {
                        const isLt2M = file.size / 1024 / 1024 < 2;
                        if (!isLt2M) {
                            this.$message.error('图片大小不能超过 2MB!');
                        }
                        return isLt2M;
                    },
                    //多图
                    uploadimgSuccess(res) {
                        this.form.images.push(res.data.path);
                    },
                    uploadimgBefore(file) {
                        const isLt2M = file.size / 1024 / 1024 < 2;
                        if (!isLt2M) {
                            this.$message.error('图片大小不能超过 2MB!');
                        }
                        return isLt2M;
                    },
                    //移除
                    handleRemove(file, fileList) {
                        this.form.images.indexOf(file.url) !=-1? this.form.images.splice(this.form.images.indexOf(file.url),1) : '';
                    },
                    //放大
                    handlePictureCardPreview(file) {
                        this.dialogImageUrl = file.url;
                        this.dialogVisible = true;
                    },
                    //返回
                    back() {
                        window.history.go(-1);
                    },
                    //更新
                    update() {
                        this.form.content = this.editor.getContent();
                        this.form._token = "{{csrf_token()}}";
                        this.$refs['form'].validate((valid) => {
                            if (!this.form.content) {
                                this.$refs.form.fields[3].error = "请填写产品详情";
                                return false;
                            }
                            if (valid) {
                                let that = this;
                                $.post("{{ route('admin.service.update') }}", this.form, function (res) {
                                    if (res.code) {
                                        that.$message.error(res.msg);
                                    } else {
                                        that.$message({
                                            type: 'success',
                                            duration: '1000',
                                            message: res.msg,
                                            onClose: () => {
                                                window.history.go(-1);
                                            }
                                        });
                                    }
                                }, 'JSON');
                            }
                        });
                    },
                }
            });
        })
    </script>
@endsection
